<template>
    <div class="login">

        <div class="login-container">
            <el-card class="login-box">
                <div class="login-title">
                    <h2>登录</h2>
                </div>
                <el-form :model="form" :rules="rules" ref="loginForm" label-position="top" class="login-form">
                    <el-form-item prop="username">
                        <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="form.password" type="password" placeholder="请输入密码"
                            prefix-icon="el-icon-lock"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleLogin" class="login-button">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { post } from '@/api/request';
import { useStore } from 'vuex';

const form = ref({
    username: '',
    password: ''
});

const rules = ref({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
});

const loginForm = ref(null);
const router = useRouter();
const store = useStore();

const isLoggedIn = computed(() => store.getters.isLoggedIn);
const u = computed(() => store.getters.user);

const handleLogin = () => {
    loginForm.value.validate((valid) => {
        if (valid) {
            post('/user/login', form.value)
                .then(response => {
                    if (response.status === 200) {
                        const user = response.data.data;
                        store.commit('setUser', user);
                        store.commit('setIsAuthenticated', true);
                        setTimeout(() => {
                            router.push('/home');
                        }, 100);
                        ElMessage.success('登录成功');
                    } else {
                        ElMessage.error(response.data.message || '登录失败，请检查用户名和密码');
                    }
                })
                .catch(error => {
                    ElMessage.error('登录失败');
                });
        } else {
            ElMessage.error('请检查输入信息');
            return false;
        }
    });
};
</script>

<style scoped>
.login {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #c1ffc1, #a9ffa9);
    overflow: hidden;
}

.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    overflow: hidden;
}

.login-box {
    width: 400px;
    padding: 40px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    border-radius: 10px;
    transition: transform 0.3s ease-in-out;
}

.login-box:hover {
    transform: scale(1.02);
}

.login-title {
    text-align: center;
    margin-bottom: 30px;
    font-size: 24px;
    color: #333;
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.label-center {
    text-align: center;
}

.el-input {
    border-radius: 5px;
    border: 1px solid #c6e4c6;
}

.el-input__inner {
    border-radius: 5px;
    border: 1px solid #c6e4c6;
}

.el-button--primary {
    background-color: #4caf50;
    border-color: #4caf50;
    border-radius: 5px;
    font-size: 16px;
    transition: background-color 0.3s, border-color 0.3s;
}

.el-button--primary:hover {
    background-color: #45a049;
    border-color: #45a049;
}

.login-button {
    width: 100%;
    height: 50px;
}
</style>